package com.dirk.happie.adapters;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.dirk.happie.R;
import com.dirk.happie.beans.CategoryBean;

public class CategoriesListAdapter extends ArrayAdapter<CategoryBean>{

    Context context; 
    int layoutResourceId;    
    
	public CategoriesListAdapter(Context context, int layoutResourceId, ArrayList<CategoryBean> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ProductHolder holder = null;
        
        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            
            holder = new ProductHolder();
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
            
            row.setTag(holder);
        }
        else
        {
            holder = (ProductHolder)row.getTag();
        }
        
        CategoryBean category = this.getItem(position);
        if(holder != null && holder.txtTitle != null)
        	holder.txtTitle.setText(category.getName());
        else
        	((TextView)row.findViewById(R.id.txtTitle)).setText(category.getName());
        
        return row;
    }
    
    static class ProductHolder
    {
        TextView txtTitle;
    }
}
